package com.prs.crm.dao.purchase.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.dao.purchase.PaymentGatherDao;
import com.prs.crm.domain.purchase.PaymentGather;
import com.prs.crm.domain.purchase.PaymentOrder;
import com.prs.crm.domain.stock.ProductStore;

public class PaymentGatherDaoImpl extends BaseDaoImpl<PaymentGather> implements
		PaymentGatherDao {

	@SuppressWarnings("unchecked")
	@Override
	public PaymentGather getPaymentGather(final PaymentOrder paymentOrder) {
		return (PaymentGather)getHibernateTemplate().execute(   
			      new HibernateCallback() {   
			        public Object doInHibernate(Session session) throws HibernateException {   
			        	Criteria criteria =  session.createCriteria(PaymentGather.class);
			        	criteria.add(Restrictions.eq("paymentOrder", paymentOrder));
			        	List<ProductStore> result = criteria.list();
			        	if (result.size()>0)
			        		return result.get(0);
			        	else
			        		return null;
			        }   
			      }   
			    );  
	}

	@Override
	public void saveOrUpdate(PaymentGather paymentGather) {
		this.getHibernateTemplate().saveOrUpdate(paymentGather);
	}

	@Override
	public PaymentGather getPaymentGatherOb(Serializable id) {
		
		return this.getHibernateTemplate().get(PaymentGather.class, id);
	}
}
